// Helpbox messages
b_help = "Bold: [b]text[/b]";
i_help = "Italic: [i]text[/i]";
u_help = "Underline: [u]text[/u]";
url_help = "URL: [url]http://url.com[/url] or [url=http://url.com]Link Name[/url]";
email_help = "Email: [email]address[/email]";
img_help = "Image: [img]http://image.url.com[/img]";
code_help = "Code: [code]text[/code]";
quote_help = "Quote: [quote]text[/quote] or [quote=author]text[/quote]";
indent_help = "Indent: [indent]text[/indent]";
align_left_help = "Left Align: [align=left]text[/align]";
align_center_help = "Centre Align: [align=center]text[/align]";
align_right_help = "Right Align: [align=right]text[/align]";
hr_help = "Horizontal Rule: [hr]text[/hr]";
html_help = "HTML: [html]HTML code[/html]";
colour_help = "Font Colour: [colour=#HexValue]text[/colour] or [colour=Red]text[/colour]";
heading_help = "Heading Style: [h1]text[/h1] or [h2]text[/h2] or [h3]text[/h3]";
default_help = "Helpbox - Hover over a button above for a description";

// From http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl)
{
	if (textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, previewArea, open, close) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;
	var scrollHeight = txtArea.scrollHeight;
	if (previewArea)
		var previewScrollHeight = previewArea.scrollHeight;
	
	if (selEnd == 1 || selEnd == 2) selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);

	txtarea.value = s1 + open + s2 + close + s3;
	txtarea.selectionStart = selEnd + open.length + close.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;
	if (previewArea)
		previewArea.scrollTop = scrollTop *  (scrollHeight / previewScrollHeight);

	return;
}

function helpBox(id, help) {
	helpbox_id = id + '_helpbox';
	document.getElementById(helpbox_id).innerHTML = eval(help + '_help');
}

function urlPrompt() {
	urlInput = prompt("Enter URL:", "http://");
	if (!urlInput) return;
	linkNameInput = prompt("Enter a name for your link:");
	if (!linkNameInput) return;
	promtCode = '[url=' + urlInput + ']' + linkNameInput + '[/url]';
	return promtCode;
}

function emailPrompt() {
	emailInput = prompt("Enter Email address:");
	if (!emailInput) return;
	promtCode = '[email]' + emailInput + '[/email]';
	return promtCode;
}

function addBBcode(id, preview_id, code, codeClose) {
	txtArea = divElement = document.getElementById(id);
	previewArea = divElement = document.getElementById(preview_id);
	txtArea.focus();
	
	if (!codeClose) codeClose = '[/' + code + ']';
	else if (codeClose == 'none') codeClose = '';
	else codeClose = '[/' + codeClose + ']';
	code = '[' + code + ']';
	
	// Gecko - The Good
	if (txtArea.selectionStart || txtArea.selectionStart == '0') {
		var startPos = txtArea.selectionStart;
		var endPos = txtArea.selectionEnd;
		
		if (startPos == endPos) {
			var promtCode = false;
			if (code == '[url]') promtCode = urlPrompt();
			else if (code == '[email]') promtCode = emailPrompt();
		}
		
		if (!promtCode) {
			mozWrap(txtArea, previewArea, code, codeClose);
		} else {
			mozWrap(txtArea, previewArea, promtCode, '');		
		}

		txtArea.focus();
	}

	// Internet Excuse - The Bad
	else if (document.selection && document.selection.createRange) {
		txtArea.focus();
		sel = document.selection.createRange();
		sel.text = code + sel.text + codeClose;
		storeCaret(txtArea);	
		txtArea.focus();
	}

	// Others - The Ugly
	else {
		txtArea.value += code + codeClose;
		txtArea.focus();
	}
	
	if (preview_id) previewParse(id, preview_id);

	return;
}
